package com.xiao.sulinges.controller;

import com.xiao.sulinges.service.ShopEsService;
import com.xiao.sulingpublic.to.BusinessTo;
import com.xiao.sulingpublic.to.ShopEsTo;
import com.xiao.sulingpublic.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author: 1322127758@qq.com
 * @date: 2022/3/22 14:15
 * @作用: 操作店铺索引
 */
@RequestMapping("/es/shop")
@RestController
public class ShopEsController {
    @Autowired
    private ShopEsService shopEsService;


    /**
     * 将商品数据保存到es中
     * @param businessTo
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody BusinessTo businessTo) throws IOException {
        if(null != businessTo){
            shopEsService.save(businessTo);
            return R.ok("保存成功!");
        }
        return R.error("保存错误!");
    }

    /**
     * 获取附近店铺集合
     * @param    shopEsTo 经度,纬度,多少km
     **/
    @RequestMapping("/list")
    R list(@RequestBody ShopEsTo shopEsTo)throws IOException{
        List<BusinessTo> businessTo = shopEsService.list(shopEsTo.getLongitude(),shopEsTo.getLatitude(),shopEsTo.getDistince());

        return R.ok().put("businessTo",businessTo);
    }


}
